<?php

class Fphoto_PhotoeController extends Core_Controller_Action_Standard
{
  public function init()
  {
     if( 0 !== ($photo_id = (int) $this->_getParam('photo_id')) &&
          null !== ($photo = Engine_Api::_()->getItem('event_photo', $photo_id)) )
      {
        if(!Engine_Api::_()->core()->hasSubject()){Engine_Api::_()->core()->setSubject($photo);}
      }

      /*else if( 0 !== ($event_id = (int) $this->_getParam('event_id')) &&
          null !== ($event = Engine_Api::_()->getItem('event', $event_id)) )
      {
        if(!Engine_Api::_()->core()->hasSubject()){Engine_Api::_()->core()->setSubject($event);}
      }
	*/
  }
  
  public function viewlightboxeventAction()
  {
        $viewer = Engine_Api::_()->user()->getViewer();
        $this->view->photo = $photo = Engine_Api::_()->core()->getSubject();
        $this->view->album = $album = $photo->getCollection();
        $this->view->event = $event = $photo->getEvent();
        $this->view->canEdit = $photo->authorization()->isAllowed($viewer, 'edit');
   	$this->view->height = $this->_getParam('height', 500);
   	$this->view->widthLeft = $this->_getParam('widthLeft', 500);
   	$this->view->width = $this->_getParam('width', 1000);
   	$this->view->canComment = $canComment =  Engine_Api::_()->core()->getSubject()->authorization()->isAllowed($viewer, 'comment');
  }

  public function quickEditAction(){
    	$title = $this->_getParam('title', '');
  		$description = $this->_getParam('description', '');
	  	$viewer = Engine_Api::_()->user()->getViewer();
	    $photo = Engine_Api::_()->core()->getSubject();
	    // do edit
	    if($title != 'Write a title...')
	    	$photo->title = $title;
	    if($description != 'Write a description...')
	    	$photo->description = $description;
	    $photo->save();

        $this->view->status = true;
	    $this->view->message = 'Comment added';
	    $this->view->body = $this->view->action('infose', 'photoe', 'fphoto', array(
	      'event_id' => $photo->event_id,
	      'photo_id' => $photo->getIdentity(),
	      'format' => 'html',
	    ));
	    $this->_helper->contextSwitch->initContext();
  }
    public function infoseAction(){
    	if( !$this->_helper->requireSubject('event_photo')->isValid()) return;
        $viewer = Engine_Api::_()->user()->getViewer();
        $this->view->photo = $photo = Engine_Api::_()->core()->getSubject();
        $this->view->album = $album = $photo->getCollection();
        $this->view->event = $event = $photo->getEvent();
        $this->view->canEdit = $photo->authorization()->isAllowed(null, 'edit');
        //$this->view->subject = Engine_Api::_()->core()->getSubject();
  }
  public function deleteAction()
  {
    $photo = Engine_Api::_()->core()->getSubject();
    $event = $photo->getParent('event');

    if( !$this->_helper->requireAuth()->setAuthParams($photo, null, 'edit')->isValid() ) {
      return;
    }

    

    // Process
    $db = Engine_Api::_()->getDbtable('photos', 'event')->getAdapter();
    $db->beginTransaction();

    try
    {
      $photo->delete();

      $db->commit();
    }

    catch( Exception $e )
    {
      $db->rollBack();
      throw $e;
    }

    return $this->_forward('success', 'utility', 'core', array(
      'messages' => array(Zend_Registry::get('Zend_Translate')->_('Photo deleted')),
      'layout' => 'default-simple',
      'parentRedirect' => $event->getHref(),
    ));
  }
}